package com.demo.design.stractural.flyweight.demo2;

import java.util.HashMap;
import java.util.Map;

public class TicketFactory {

    private static Map<String, Ticket> ticketMap = new HashMap<>();

    public Ticket getTicket(String from, String to) {
        String key = from + "->" + to;
        if (ticketMap.containsKey(key)) {
            System.err.println("using 缓存");
            Ticket ticket = ticketMap.get(key);
            return ticket;
        }
        System.err.println("初始化");
        Ticket ticket = new RealTicket(from, to);
        ticketMap.put(key, ticket);
        return ticketMap.get(key);
    }
}
